import request from "@/utils/request";
import { ApiReturn } from "@/utils/request";
import md5 from "js-md5";

export class UserLogin {
  userId: string;
  password: string;
  constructor(userId: string, password: string) {
    this.userId = userId;
    this.password = password;
  }
}

interface UserInfo {
  picture: string;
  telephone: string;
  username: string;
}

export class MenuResp {
  icon!: string;
  id!: number;
  path!: string;
  title!: string;
  childrens: MenuResp[];

  constructor() {
    this.childrens = [];
  }
}

export class Account {
  id!: number;
  username!: string;
  telephone!: string;
  userType!: number;
  userTypeValue!: string;
  addpower!: number;
  addpowerValue!: string;
  createTime!: string;
  updateTime!: string;
  switchLoading!: boolean;
}

export class AccountResp {
  data!: Account[];
  total!: number;
}

export interface Student {
  address?: string;
  classId?: string;
  className?: string;
  collageName?: string;
  collegeId?: string;
  createTime?: string;
  majarId?: string;
  majorName?: string;
  picture?: string;
  studentCardId?: string;
  studentGender?: string;
  studentName?: string;
  studentNum?: string;
  telephone?: string;
  updateTime?: string;
  username?: string;
}
export class StudentResp {
  data!: Student[];
  total!: number;
}

export interface Collageinfo {
  collageName: string;
  collageNum: string;
}
export interface Major {
  majorName: string;
  majorNum: string;
}

export interface Clazz {
  className: string;
  classNum: string;
}

export interface Teacher {
  academicDegree?: string;
  createTime?: string;
  education?: string;
  picture?: string;
  teacherCardId?: string;
  teacherGender?: string;
  teacherName?: string;
  teacherNum?: string;
  telephone?: string;
  updateTime?: string;
  username?: string;
}
class TeacherResp {
  data!: Teacher[];
  total!: number;
}
// 用户相关
export class UserService {
  static async login(params: UserLogin): ApiReturn<any> {
    return request.post("/user/login", {
      telephone: params.userId,
      password: md5(params.password),
    });
  }

  static async getUserInfo(): ApiReturn<UserInfo> {
    return request.get("user/userInfo");
  }

  static async getMenu(): ApiReturn<MenuResp[]> {
    return request.get("/user/view");
  }

  static async userList(params: any): ApiReturn<AccountResp> {
    return request.get("/user/query", { params });
  }
  static async deleteAccount(ids: number[]): ApiReturn<any> {
    return request.delete("/user/del", { data: ids });
  }
  static async addAccount(data: Account): ApiReturn<any> {
    return request.post("/user/put", data);
  }

  static async listStudent(params: any): ApiReturn<StudentResp> {
    return request.get("/students", { params });
  }

  static async saveStudent(data: any, type: string): ApiReturn<any> {
    if (type == "add") {
      return request.post("/students", data);
    } else {
      return request.put(`/students/${data.studentNum}`, data);
    }
  }

  static async getCollageinfo(): ApiReturn<Collageinfo[]> {
    return request.get("/collageinfo");
  }

  static async getCollageinfoById(id: string): ApiReturn<Major[]> {
    return request.get(`/collageinfo/${id}`);
  }

  static async getMajorinfoClass(id: string): ApiReturn<Clazz[]> {
    return request.get(`/majorinfo/${id}`);
  }

  static async getStudentById(id: string): ApiReturn<Student> {
    return request.get(`/students/${id}`);
  }

  static async unbind(telephone: string): ApiReturn<any> {
    return request.patch(`/user/unbind/${telephone}`);
  }

  static async listTeacher(params: any): ApiReturn<TeacherResp> {
    return request.get("/query", { params });
  }

  static async saveTeacher(data: any, type: string): ApiReturn<any> {
    if (type == "add") {
      return request.post("/teachers", data);
    } else {
      return request.put(`/teachers/${data.teacherNum}`, data);
    }
  }

  static async getTeacherById(id: string): ApiReturn<Teacher> {
    return request.get(`/teachers/${id}`);
  }
}
